/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-6-4下午07:20:24
 */
package hit.edance.campusmobile.chat.activity;

import java.util.List;

import hit.edance.campusmobile.chat.R;
import hit.edance.campusmobile.chat.adapter.ChattingAdapter;
import hit.edance.campusmobile.chat.database.DBChatHistoryHelper;
import hit.edance.campusmobile.chat.model.ActivityKiller;
import hit.edance.campusmobile.chat.model.ChatMessage;
import hit.edance.campusmobile.chat.util.ConstantUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**   
 * 聊天记录
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.activity
 * @Class ActivityChatHistory
 * @Date 2012-6-4 下午07:20:24
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class ActivityChatHistory extends Activity {
	/**
	 * menu 清除聊天历史
	 */
	public static final int ITEM0 = Menu.FIRST; 
	/**
	 * 聊天历史adapter
	 */
	private ChattingAdapter chatHistoryAdapter;
	/**
	 * 聊天历史list
	 */
	private List<ChatMessage> messages;
	/**
	 * 聊天历史ListView对象
	 */
	private ListView list;
	/**
	 * title bar上的textview
	 */
	private TextView tvname;
	/**
	 * 聊天记录参与者的JID
	 */
	private String jid;
	/**
	 * ActivityChatHistory's handler
	 */
	private Handler myhandler;
	/**
	 * 数据库对象
	 */
	private DBChatHistoryHelper db;
	
	/* @see android.app.Activity#onCreate(android.os.Bundle)
	 *onCreate
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		ActivityKiller.addActivity(hashCode(), this);
		//设置全屏模式
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_chathistory);
		//设置title
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.chat_chatting_title_bar);
		myhandler = new Handler();
		initActivity();
	}
	
	/* @see android.app.Activity#onNewIntent(android.content.Intent)
	 *onNewIntent
	 */
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		setAdapterForThis();
	}

	private void initActivity(){
		list = (ListView) findViewById(R.id.chathistorylist);
		jid = getIntent().getStringExtra("jid");
		tvname = (TextView)findViewById(R.id.chatting_contact_name);
		//设置title为用户昵称
		tvname.setText(getIntent().getStringExtra("nickname"));
		db = new DBChatHistoryHelper(this);
		setAdapterForThis();
	}
	private void setAdapterForThis() {
		messages = db.getAllChatItems(ConstantUtil.JID,jid);
		chatHistoryAdapter = new ChattingAdapter(this, messages);
		list.setAdapter(chatHistoryAdapter);
	}
	/* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 *onCreateOptionsMenu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, ITEM0, 0, getResources().getString(R.string.chat_clearchathistory));
		menu.findItem(ITEM0).setIcon(R.drawable.chat_ic_menu_close_clear_cancel);
		return true;
	}

	/* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 *onOptionsItemSelected
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case ITEM0:
				actionClickMenuItem0();
				break;
		}
		return super.onOptionsItemSelected(item);
	}
	/**
	 * 清除聊天记录
	 */
	private void actionClickMenuItem0() {
		if(db.clearHistory(ConstantUtil.JID,jid)){
			myhandler.post(new Runnable(){

				public void run() {
					messages.clear();
					chatHistoryAdapter.notifyDataSetChanged();
				}

			});
			Toast.makeText(ActivityChatHistory.this, "清除成功！", Toast.LENGTH_SHORT);
		}else{
			Toast.makeText(ActivityChatHistory.this, "清除失败！", Toast.LENGTH_SHORT);
		}
	}

	/* @see android.app.Activity#onDestroy()
	 *onDestroy
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		ActivityKiller.killActivityByHashCode(hashCode());
	}
	
}
